package com.liuyi.netty.im.test.util;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.liuyi.netty.im.commons.codes.CodeConst;
import com.liuyi.netty.im.commons.codes.LimProtocol;
import com.liuyi.netty.im.commons.codes.data.LimData;
import com.liuyi.netty.im.commons.codes.data.event.message.playload.Playload;
import com.liuyi.netty.im.commons.codes.unusual.LimException;
import io.netty.channel.Channel;

/**
 * Im 操作工具类
 *
 * @author Mr.Fmy
 * @since 2021-11-27
 */
public class ImHelp {

    /**
     * 获取精确到秒的时间戳（提前）
     */
    public static String getInAdvanceTimeStamp(long seconds) {
        return String.valueOf((System.currentTimeMillis() / 1000) - seconds);
    }

    /**
     * 获取精确到秒的时间戳
     */
    public static String getTimeStamp() {
        return String.valueOf(System.currentTimeMillis() / 1000);
    }

    /**
     * 解码playload
     *
     * @param playload playload
     * @param tClass   t类
     * @return {@link T}
     */
    public static <T extends Playload> T decodePlayload(Object playload, Class<T> tClass) {
        return JSONObject.parseObject(encodePlayload(playload), tClass);
    }

    /**
     * 解码playload
     *
     * @param playload playload
     * @param tClass   t类
     * @return {@link T}
     */
    public static <T extends Playload> T decodePlayload(byte[] playload, Class<T> tClass) {
        return JSONObject.parseObject(playload, tClass);
    }


    /**
     * 编码
     *
     * @param playload playload
     * @return {@link byte[]}
     */
    public static byte[] encodePlayload(Object playload) {
        if (playload == null) {
            return null;
        }
        return JSONObject.toJSONBytes(playload);
    }

    /**
     * 得到json序列化格式的协议
     *
     * @param command 命令
     * @param content 内容
     * @return {@link LimProtocol}
     */
    public static <T extends LimData> LimProtocol getJsonProtocol(CodeConst.Command command, T content) {
        LimProtocol protocol = new LimProtocol();
        protocol.setVersion(CodeConst.MIN_VERSION);
        protocol.setCommand(command);
        protocol.setDataType(CodeConst.CONTENT_TYPE_JSON);
        protocol.setData(content);
        return protocol;
    }


    public static String getChannelIp(Channel channel) {
        return channel.remoteAddress().toString();
    }

    public static String sessionId(CodeConst.SessionType sessionType, String toId) {
        return sessionType.name() + "@" + toId;
    }

    public static CodeConst.SessionType sessionIdToType(String sessinId) {
        if (StrUtil.isNotEmpty(sessinId)) {
            String marking = sessinId.split("@")[0];
            if (StrUtil.isEmpty(marking)) {
                throw new LimException("sessionId Nonconformity to specification");
            }
            CodeConst.SessionType[] values = CodeConst.SessionType.values();
            for (CodeConst.SessionType value : values) {
                if (marking.equals(value.name())) {
                    return value;
                }
            }
        }
        throw new LimException("sessionId Nonconformity to specification");
    }

    public static String sessionIdToId(String sessinId) {
        if (StrUtil.isNotEmpty(sessinId)) {
            return sessinId.split("@")[1];
        }
        throw new LimException("sessionId Nonconformity to specification");
    }
}
